package io.renren.modules.app.message.handler;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import io.renren.modules.app.message.MsgType;
import io.renren.modules.app.message.constant.JdVopConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author :
 * @version V1.0
 * @Project:
 * @Package
 * @Description: 消息轮询管理器
 * @date
 **/
@Component
@Slf4j
@Profile(value = {"prod"})
public class MessageLoopManager  implements CommandLineRunner {

//    @Autowired
//    private JdVopApiHandler jdVopApiHandler;

    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public void run(String... args) {
        Map<String,JdVopMessageHandler> handlers=applicationContext.getBeansOfType(JdVopMessageHandler.class);
        for (Map.Entry<String,JdVopMessageHandler> handler:handlers.entrySet()) {
            JdVopMessageHandler msgHandler= handler.getValue();
//            log.info("未创建{}消息轮询器线程，请取消注释",handler.getKey());
            log.info("创建消息轮询器：{}",handler.getKey());
//            ThreadUtil.newThread(new MessageLoopHandler(jdVopApiHandler,msgHandler,getMessageType(handler.getKey())),handler.getKey(),true).start();
            ThreadUtil.newThread(new MessageLoopHandler(msgHandler,getMessageType(handler.getKey())),handler.getKey(),true).start();
        }
    }

    /**
     *  配置每个消息轮圈器又要拉取的消息类型
     * @param msgHandler
     * @return
     */
    private String getMessageType(String msgHandler){
        switch  (msgHandler) {
//            case OrderMessageHandler.BeanName:
//                return StrUtil.join(JdVopConstant.COMMON_DELIMITER, MsgType.Order.CREATED,MsgType.Order.DISPATCH,MsgType.Order.COMPLETE,MsgType.Order.AFTER_SALE);
            case ProductMessageHandler.BeanName:
                return StrUtil.join(JdVopConstant.COMMON_DELIMITER,MsgType.Product.STATE,MsgType.Product.DETAIL);
//            case PayMessageHandler.BeanName:
//                return StrUtil.join(JdVopConstant.COMMON_DELIMITER, MsgType.Pay.FAILED,MsgType.Pay.EXCEED_7D);
            case ProductPriceMessageHandler.BeanName:
                return StrUtil.join(JdVopConstant.COMMON_DELIMITER,MsgType.Product.PRICE);
//            case PoolMessageHandler.BeanName:
//                return StrUtil.join(JdVopConstant.COMMON_DELIMITER,MsgType.Pool.CHANGE,MsgType.Pool.STATE);
            default:
                return null;
        }
    }
}
